﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EPiServer.Core;
using EPiServer.SpecializedProperties;

namespace EPiBoost.Core.Helpers
{
	public static class ContentPropertyHelpers
	{
		  
		  
		/// <summary>
		/// Gets a string property.
		/// </summary>
		/// <param name="pData"></param>
		/// <param name="propertyName"></param>
		/// <returns>Returns the value or an empty string if it is null</returns>
		public static string GetStringProperty(this PageData pData, string propertyName)
		{
			return pData.GetStringProperty(propertyName, String.Empty);
		}

		/// <summary>
		/// Gets a string property.
		/// </summary>
		/// <param name="pData"></param>
		/// <param name="propertyName"></param>
		/// <param name="defaultValue"></param>
		/// <returns>Returns the value or a default value if it is null</returns>
		public static string GetStringProperty(this PageData pData, string propertyName, string defaultValue)
		{
			return pData[propertyName] as string ?? defaultValue;
		}

		/// <summary>
		/// Gets an integer property.
		/// </summary>
		/// <param name="pData"></param>
		/// <param name="propertyName"></param>
		/// <returns>The value or 0 if it is null</returns>
		public static int GetIntProperty(this PageData pData, string propertyName)
		{
			return pData.GetIntProperty(propertyName, 0);
		}

		/// <summary>
		/// Gets an integer property. 
		/// </summary>
		/// <param name="pData"></param>
		/// <param name="propertyName"></param>
		/// <param name="defaultValue"></param>
		/// <returns>Returns the value or a default value if it is null.</returns>
		public static int GetIntProperty(this PageData pData, string propertyName, int defaultValue)
		{
			object data = pData[propertyName];

			if (data is string)
			{
				int val = new int();

				if (int.TryParse((string)data, out val))
				{
					return val;
				}
				else
				{
					return defaultValue;
				}
			}

			return data as int? ?? defaultValue;
		}

		/// <summary>
		/// Gets a double property
		/// </summary>
		/// <param name="pData"></param>
		/// <param name="propertyName"></param>
		/// <returns>Returns the value or 0.0 if null.</returns>
		public static double GetDoubleProperty(this PageData pData, string propertyName)
		{
			return pData.GetDoubleProperty(propertyName, 0.0);
		}

		/// <summary>
		/// Gets a double property
		/// </summary>
		/// <param name="pData"></param>
		/// <param name="propertyName"></param>
		/// <param name="defaultValue"></param>
		/// <returns>Returns the value or a default value if null.</returns>
		public static double GetDoubleProperty(this PageData pData, string propertyName, double defaultValue)
		{
			object data = pData[propertyName];

			if (data is string)
			{
				double val = new double();
				if (double.TryParse((string)data, out val))
				{
					return val;
				}
				else
				{
					return defaultValue;
				}
			}

			return data as double? ?? defaultValue;
		}

		/// <summary>
		/// Gets a bool value.
		/// </summary>
		/// <param name="pData"></param>
		/// <param name="propertyName"></param>
		/// <returns>Returns the value or false if it is null</returns>
		public static bool GetBoolProperty(this PageData pData, string propertyName)
		{
			return pData.GetBoolProperty(propertyName, false);
		}

		/// <summary>
		/// Gets a bool value.
		/// </summary>
		/// <param name="pData"></param>
		/// <param name="propertyName"></param>
		/// <param name="defaultValue"></param>
		/// <returns>Returns the value or a default value if it is null</returns>
		public static bool GetBoolProperty(this PageData pData, string propertyName, bool defaultValue)
		{
			return pData[propertyName] as bool? ?? defaultValue;
		}

		/// <summary>
		/// Gets a datetime property
		/// </summary>
		/// <param name="pData"></param>
		/// <param name="propertyName"></param>
		/// <returns>Returns the value or DateTime.Now if it is null</returns>
		public static DateTime? GetDateTimeProperty(this PageData pData, string propertyName)
		{
			return pData.GetDateTimeProperty(propertyName, DateTime.Now);
		}

		/// <summary>
		/// Gets a datetime property
		/// </summary>
		/// <param name="pData"></param>
		/// <param name="propertyName"></param>
		/// <param name="defaultValue"></param>
		/// <returns>Returns the value or a default value if it is null</returns>
		public static DateTime? GetDateTimeProperty(this PageData pData, string propertyName, DateTime? defaultValue)
		{
			return pData[propertyName] as DateTime? ?? defaultValue;
		}

		public static DateTime? GetDateTimeProperty(this PageData pData, string propertyName, bool returnNullable)
		{
			return returnNullable ? pData[propertyName] as DateTime? : pData.GetDateTimeProperty(propertyName);
		}

		/// <summary>
		/// Gets a PageReference from a property
		/// </summary>
		/// <param name="pData"></param>
		/// <param name="propertyName"></param>
		/// <returns>Returns PageReference or null if there was a problem</returns>
		public static PageReference GetPageReferenceProperty(this PageData pData, string propertyName)
		{
			return pData[propertyName] as PageReference;
		}

		/// <summary>
		/// Gets an EPiServer property of the specified type.
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <param name="pData"></param>
		/// <param name="propertyName"></param>
		/// <param name="defaultValue"></param>
		/// <returns>Returns the value or null if it cannot be cast or does not exist</returns>
		public static T GetProperty<T>(this PageData pData, string propertyName, T defaultValue) where T : class
		{
			return pData[propertyName] as T ?? defaultValue;
		}

		/// <summary>
		/// Gets the link for the page
		/// </summary>
		public static string GetPageUrl(this PageData pData)
		{
			return pData.LinkURL;
		}

		/// <summary>
		/// Get the link target _blank or _top
		/// </summary>
		/// <param name="pData"></param>
		/// <returns></returns>
		public static string GetLinkTarget(this PageData pData)
		{
			if (pData["PageTargetFrame"] != null)
			{
				return (pData["PageTargetFrame"].ToString() == "1") ? "_blank" : "_top"; 
			}
			return string.Empty;
		}
	  

	}
}
